<?php
declare (strict_types = 1);

namespace app\command;

use app\api\model\EverydayPerformance;
use app\api\model\Orders;
use app\api\model\User;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use think\facade\Log;

class DirectorRewardForMonthCommand extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('director_reward_for_month')
            ->setDescription('the director_reward_for_month command');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $output->writeln('director_reward_for_month');

//        $this->grantRewardForMonth();

        $this->grantHonorDirectorReward();

//        Db::startTrans();
//        try {
//
//
//            Db::commit();
//        } catch (\Exception $exception) {
//
//            Db::rollback();
//
//            Log::error('director_reward_for_month:'.json_encode(['message' => $exception->getMessage(), 'file' => $exception->getFile(), 'line' => $exception->getLine()], JSON_UNESCAPED_UNICODE));
//            return false;
//        }
    }

    public function grantRewardForMonth()
    {
        $user_lists = User::where('status', 'on')
            ->where('pid', 2)
            ->select();
//        dd($user_lists);

        foreach ($user_lists as $user)
        {
            $str = '%|' . $user['id'] . '|2|%';
            $children_lists = User::where('status', 'on')
                ->whereLike('parent_ids', $str)
                ->where('level_id', '>=', 5)
//                ->order('LENGTH(parent_ids) DESC')
                ->order(Db::raw('LENGTH(parent_ids) DESC'))
                ->select();
            $start_performance = 0;
            foreach ($children_lists as $children)
            {
//                echo $children['id'] . ':' . $children['parent_ids'] . ',';

                $current_children_performance = EverydayPerformance::whereLike('user_ids', '%|' . $children['id'] . '|%')
                    ->where('is_delete', 0)
                    ->whereMonth('create_time')
                    ->sum('pay_money');
                $remain_children_performance = bcsub((string)$current_children_performance, (string)$start_performance);
                $reward = self::calcReward($remain_children_performance);
                if($reward > 0){
                    //induce($user_id, $amount, $alias = 'score', $type = 'shop_pay', $des = '')
                    User::induce($user['id'], $reward, 'score', 'partner_reward_for_month');
                }
                $start_performance = $current_children_performance;
            }
            $arr = [
                $user['id'],
                2
            ];
            foreach ($arr as $item)
            {
                $current_user = User::where('id', $item)->find();
                if($current_user['level_id'] >= 5)
                {
                    $current_user_performance = EverydayPerformance::whereLike('user_ids', '%|' . $current_user['id'] . '|%')
                        ->where('is_delete', 0)
                        ->whereMonth('create_time')
                        ->sum('pay_money');
                    $remain_user_performance = bcsub((string)$current_user_performance, (string)$start_performance);
                    $reward = self::calcReward($remain_user_performance);
                    if($reward > 0){
                        //induce($user_id, $amount, $alias = 'score', $type = 'shop_pay', $des = '')
                        User::induce($current_user['id'], $reward, 'score', 'partner_reward_for_month');
                    }
                    $start_performance = $current_user_performance;
                }
            }
        }
        return true;
    }

    public static function calcReward($remain_performance)
    {
        //计算相应级别的奖金
        $reward = 0;
        if($remain_performance <= 5000000)
        {
            $reward = bcdiv(bcmul($remain_performance, '2'), '100');
        }elseif ($remain_performance > 5000000 && $remain_performance <= 10000000)
        {
            $reward = bcdiv(bcmul($remain_performance, '3'), '100');
        }elseif ($remain_performance > 10000000 && $remain_performance <= 20000000)
        {
            $reward = bcdiv(bcmul($remain_performance, '4'), '100');
        }elseif ($remain_performance > 20000000 && $remain_performance <= 40000000)
        {
            $reward = bcdiv(bcmul($remain_performance, '5'), '100');
        }elseif ($remain_performance > 40000000 && $remain_performance <= 80000000)
        {
            $reward = bcdiv(bcmul($remain_performance, '6'), '100');
        }elseif ($remain_performance > 80000000 && $remain_performance <= 120000000)
        {
            $reward = bcdiv(bcmul($remain_performance, '7'), '100');
        }elseif ($remain_performance > 120000000 && $remain_performance <= 200000000)
        {
            $reward = bcdiv(bcmul($remain_performance, '8'), '100');
        }elseif ($remain_performance > 200000000 && $remain_performance <= 300000000)
        {
            $reward = bcdiv(bcmul($remain_performance, '9'), '100');
        }elseif ($remain_performance > 300000000)
        {
            $reward = bcdiv(bcmul($remain_performance, '10'), '100');
        }
        return $reward;
    }

    public function grantHonorDirectorReward()
    {
        //荣誉董事补贴
        $total_goods_num = Orders::where('status', 'payed')->whereMonth('create_time')->sum('goods_num');
        $user_lists = User::where('level_id', 7)->select();
        $count = count($user_lists);
        $amount = 0;
        $arr = [];
        $honor_director_reward = 500;//荣誉董事补贴
        if($count > 0)
        {
            $amount = bcdiv(bcmul((string)$honor_director_reward, (string)$total_goods_num), (string)$count);
            foreach ($user_lists as $user)
            {
                //induce($user_id, $amount, $alias = 'score', $type = 'shop_pay', $des = '')
                User::induce($user['id'], $amount, 'score', 'shop_honor_director_allowances');
            }
        }
        return true;
    }

    public function old2()
    {
        $user_lists = User::where('status', 'on')
            ->where('level_id', '>', 4)->select();
//        dd($user_lists);

        foreach ($user_lists as $user)
        {
            $total_performance = EverydayPerformance::whereLike('user_ids', '%|' . $user['id'] . '|%')
                ->where('is_delete', 0)
                ->whereMonth('create_time')
                ->sum('pay_money');
            $reward = 0;
            if($total_performance <= 5000000)
            {
                $reward = bcdiv(bcmul((string)$total_performance, '2'), '100');
            }elseif ($total_performance > 5000000 && $total_performance <= 10000000)
            {
                $reward = bcdiv(bcmul((string)$total_performance, '3'), '100');
            }elseif ($total_performance > 10000000 && $total_performance <= 20000000)
            {
                $reward = bcdiv(bcmul((string)$total_performance, '4'), '100');
            }elseif ($total_performance > 20000000 && $total_performance <= 40000000)
            {
                $reward = bcdiv(bcmul((string)$total_performance, '5'), '100');
            }elseif ($total_performance > 40000000 && $total_performance <= 80000000)
            {
                $reward = bcdiv(bcmul((string)$total_performance, '6'), '100');
            }elseif ($total_performance > 80000000 && $total_performance <= 120000000)
            {
                $reward = bcdiv(bcmul((string)$total_performance, '7'), '100');
            }elseif ($total_performance > 120000000 && $total_performance <= 200000000)
            {
                $reward = bcdiv(bcmul((string)$total_performance, '8'), '100');
            }elseif ($total_performance > 200000000 && $total_performance <= 300000000)
            {
                $reward = bcdiv(bcmul((string)$total_performance, '9'), '100');
            }elseif ($total_performance > 300000000)
            {
                $reward = bcdiv(bcmul((string)$total_performance, '10'), '100');
            }
//            dd($reward);
            //induce($user_id, $amount, $alias = 'score', $type = 'shop_pay', $des = '')
            User::induce($user['id'], $reward, 'score', 'partner_reward_for_month');
        }
    }
}
